package org.huatek.anders.exception;

import lombok.extern.slf4j.Slf4j;
import org.huatek.anders.utils.R;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author anders
 * @date 2018-12-05 13:49
 **/
@Slf4j
@RestControllerAdvice
public class GlobleExceptionHandler {
    /**
     * 处理自定义异常
     */
    @ExceptionHandler(ApiException.class)
    public R handleApiException(ApiException e){
        log.error("ApiException:"+e.getMessage(), e);
        return R.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException(DuplicateKeyException e){
        log.error(e.getMessage(), e);
        return R.error("数据库中已存在该记录");
    }

    /*@ExceptionHandler(AuthorizationException.class)
    public R handleAuthorizationException(AuthorizationException e){
        log.error(e.getMessage(), e);
        return R.error("没有权限，请联系管理员授权");
    }*/

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e){
        log.error("Exception:"+e.getMessage(), e);
        return R.error();
    }
}
